package org.gdkm.controller;

import org.apache.commons.lang.StringUtils;
import org.gdkm.domin.TbUser;
import org.gdkm.json.MapperUtils;
import org.gdkm.service.LoginService;
import org.gdkm.service.consumer.RedisService;
import org.gdkm.util.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    LoginService loginService;

    @Autowired
    RedisService redisService;



    @GetMapping("login")
    public String login(HttpServletRequest request, Model model, @RequestParam(required = false) String url) {
        String token = CookieUtils.getCookieValue(request, "token");
        if (StringUtils.isNotBlank(token)) {
            String username = redisService.get(token);
            if (StringUtils.isNotBlank(username)) {
                String json = redisService.get(username);
                if (StringUtils.isNotBlank(json)) {
                    try {
                        TbUser tbUser = MapperUtils.json2pojo(json, TbUser.class);
                        if (tbUser != null) {
                            if (StringUtils.isNotBlank(url)) {
                                System.out.println("url = " + url);
                                return "redirect:" + url;
                            }
                        }
                        model.addAttribute("tbUser", tbUser);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (StringUtils.isNotBlank(url)){
            model.addAttribute("url",url);
        }
        return "login";
    }

    @PostMapping(value = "login")
    public String checkLogin(String username, String password,
                        @RequestParam(required = false) String url,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        RedirectAttributes redirectAttributes) {

        TbUser tbuser = loginService.login(username, password);

        if (tbuser == null) {

            redirectAttributes.addFlashAttribute("message", "用户名或密码错误，请重新输入");

        } else {
            String token = UUID.randomUUID().toString();
            String result = redisService.put(token, username);
            if (StringUtils.isNotBlank(result) && "success".equals(result)) {
                CookieUtils.setCookie(request, response, "token", token);
                if (StringUtils.isNotBlank(url)) {
                    return "redirect:" + url;
                }
            } else {
                redirectAttributes.addFlashAttribute("message", "服务器异常，请稍后尝试");
            }

        }
        return "redirect:/login";

    }
    @GetMapping(value = "layout")
    public String layout(HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes){
        String token = CookieUtils.getCookieValue(request, "token");
        String json = redisService.get(token);
        String userKey = redisService.delete(json);
        String usernameKey = redisService.delete(token);
        CookieUtils.deleteCookie(request,response,"token");
        if (StringUtils.equals(userKey,"true")&&StringUtils.equals(usernameKey,"true")){
        redirectAttributes.addFlashAttribute("message", "注销成功");
        }
        return "redirect:/login";
    }


}
